package client.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import client.io.ClientIO;
import domain.action.Action;
import domain.exception.ValidationException;
import domain.patient.Patient;
import domain.session.Request;
import domain.session.Response;
import domain.user.User;

public class PatientService {
	
	public static Patient getPatient(final Integer userId) {
		JSONObject requestJson = new JSONObject();
		requestJson.put("patientId", userId);
		ClientIO.sendRequest(new Request(Action.GET_PATIENT, requestJson));

		Response response = ClientIO.getResponse();
		JSONObject responseJson = response.getResponse();
		return new Patient(response.getJSON());
	}
	
	public static List<User> findPatients(final String s) {
		JSONObject requestJson = new JSONObject();
		requestJson.put("query", s);
		ClientIO.sendRequest(new Request(Action.FIND_PATIENTS, requestJson));

		Response response = ClientIO.getResponse();
		JSONObject responseJson = response.getResponse();
		JSONArray patientArray = responseJson.getJSONArray("patients");
		
		List<User> patients = new ArrayList<User>();
		
		for (int i = 0; i < patientArray.length(); i++) {
			patients.add(new Patient(patientArray.getJSONObject(i)));
		}
		
		return patients;
		
	}
	
	public static List<Patient> getAllPatients() {
		JSONObject requestJson = new JSONObject();
		ClientIO.sendRequest(new Request(Action.GET_ALL_PATIENTS, requestJson));

		Response response = ClientIO.getResponse();
		JSONObject responseJson = response.getResponse();
		JSONArray patientArray = responseJson.getJSONArray("patients");
		
		List<Patient> patients = new ArrayList<Patient>();
		
		for (int i = 0; i < patientArray.length(); i++) {
			patients.add(new Patient(patientArray.getJSONObject(i)));
		}
		
		return patients;
	}
	
	public static void modifyDOB(final Integer userId, final String dobText) throws ValidationException {
		JSONObject requestJson = new JSONObject();
		requestJson.put("userId", userId);
		requestJson.put("date", dobText);
		ClientIO.sendRequest(new Request(Action.MODIFY_DOB, requestJson));

		Response response = ClientIO.getResponse();
		
		if (!response.isSuccessful()) {
			ValidationException ve = new ValidationException(response.getResponse().getString("msg"));
			throw ve;
		}
	}

	public static void modifyAllergies(final int userId, final String text) {
		JSONObject requestJson = new JSONObject();
		requestJson.put("userId", userId);
		requestJson.put("allergies", text);
		ClientIO.sendRequest(new Request(Action.MODIFY_ALLERGIES, requestJson));

		Response response = ClientIO.getResponse();
		JSONObject responseJson = response.getResponse();
	}

	public static void modifyInsurance(final int userId, final String text) {
		JSONObject requestJson = new JSONObject();
		requestJson.put("userId", userId);
		requestJson.put("insurance", text);
		ClientIO.sendRequest(new Request(Action.MODIFY_INSURANCE, requestJson));

		Response response = ClientIO.getResponse();
		JSONObject responseJson = response.getResponse();
	}

	public static void modifyContact(final int userId, final String address, final String email) throws ValidationException{
		JSONObject requestJson = new JSONObject();
		requestJson.put("userId", userId);
		requestJson.put("address", address);
		requestJson.put("email", email);
		ClientIO.sendRequest(new Request(Action.MODIFY_CONTACT, requestJson));

		Response response = ClientIO.getResponse();
		if (!response.isSuccessful()) {
			ValidationException ve = new ValidationException(response.getResponse().getString("msg"));
			throw ve;
		}
	}

}
